package com.yunang.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunang.oa.common.R;
import com.yunang.oa.entity.OaUser;
import com.yunang.oa.service.OaUserPlusService;
import com.yunang.oa.service.OaUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.lang.*;
import java.util.List;

/**
 * @ProjectName oa
 * @Author EsnCode
 * @date 2021/4/30
 */


@RestController
@RequestMapping("/v1/oa/user")
public class OaUserController {

    private final OaUserService oaUserService;

    private final OaUserPlusService oaUserPlusService;

    @Autowired
    public OaUserController(OaUserService oaUserService, OaUserPlusService oaUserPlusService) {
        this.oaUserService = oaUserService;
        this.oaUserPlusService = oaUserPlusService;
    }

//    //以前用的查找所有用户的方法（把所有用户的数据放在List中，再进行返回）
//    @ApiOperation(value = "查找所有的用户1")
//    @GetMapping("/findAll")
//    public List<OaUser> getOaUserList() {
//        return oaUserService.getOaUserList();
//    }

    //现在用的查找所有用户的方法（用已经封装好的方法R进行返回）


    @ApiOperation(value = "增加用户")
    @PostMapping("/add")
    public R addOaUser(@RequestBody(required = true) OaUser user) {

        Integer i = oaUserService.insertOaUser(user);
        return R.ok(i > 0 ? "success" : "failed");

    }


    @ApiOperation(value = "通过用户id进行删除用户")
    @DeleteMapping("/remove/{id}")
    public R removeById(@PathVariable("id") Long id) {

        boolean remove = oaUserPlusService.removeById(id);

        return R.ok(remove ? "success" : "error");
    }

    @ApiOperation(value = "更改+保存")
    @PostMapping("/save")
    public R saveOaUser(@RequestBody(required = true) OaUser user) {

        Long id = user.getId();
        OaUser oaUser = oaUserPlusService.getById(id);
        BeanUtils.copyProperties(user, oaUser);
        boolean save = oaUserPlusService.saveOrUpdate(oaUser);

        return R.ok(save ? "success" : "failed");

    }

    @ApiOperation(value = "查找所有的用户")
    @GetMapping("/findAll")
    public R getOaUserRList() {
        return R.ok(oaUserService.getOaUserList()) ;
    }

    @ApiOperation(value = "通过用户id查找用户")
    @GetMapping("/getById/{id}")
    public R getOaUserById(@PathVariable("id") Long id) {
        OaUser user = oaUserService.getOaUserById(id);
        return R.ok(user);
    }


    @ApiOperation(value = "通过用户名查找用户")
    @GetMapping("/getByName/{name}")
    public R selectByName(@PathVariable("name") String name){

        QueryWrapper<OaUser> wrapper = new QueryWrapper<>();

        //select * from oa_user where user_name = #{name}
//        wrapper.eq("user_name",name);
        wrapper.like("user_name",name);
        //    wrapper.like()
        List<OaUser> list = oaUserPlusService.list(wrapper);

        return  R.ok(list);

    }





//    @ApiOperation(value = "显示指定部门的所有成员")        /*这个暂定，暂时因技术层面无法正常实现111111111*/
//    @GetMapping("/get/{departmentname}")
//    public R selectByDepartmentName(@PathVariable("departmentName") String departmentName){
//        return R.ok();
//    }



    @ApiOperation(value = "页面显示限制")
    @GetMapping("/page/{current}/{limit}")
    public R findOaUserPage(@PathVariable("current") Long current,
                            @PathVariable("limit") Long limit,
                            @PathVariable(required = false) String userName) {

        Page<OaUser> page = new Page<>(current, limit);
        QueryWrapper<OaUser> wrapper = new QueryWrapper<>();

        //select * from oaUser where user_name like '%userName

        if (!StringUtils.isEmpty(userName)) {
            wrapper.like("user_name", userName);
        }

        Page<OaUser> resPage = oaUserPlusService.page(page, wrapper);

        return R.ok(resPage);


    }
}
